import os
import re

"""
:param 清洗文件路径
:return 按时间顺序排列好的文件路径名 
"""


def filter_file_path():
    all_file = []
    for i in os.walk('./data/stu_data'):
        all_file.append(i)
    dir_name = all_file[0][1]
    max_compare_times = 3
    dir_name = sort_by_date(dir_name, max_compare_times)
    all_file = all_file[1:]
    month_data_name = []
    final_month_name = []
    file_with_path = []
    for i in all_file:
        month_data_name.append(i)
    for i in month_data_name:
        final_month_name.append(i[-1])

    dir_len = len(dir_name)
    max_compare_times = 4
    for i in range(0, dir_len):
        final_month_name[i] = sort_by_date(final_month_name[i], max_compare_times)

    for i in range(0, len(dir_name)):
        for j in range(0, len(final_month_name[i])):
            final_month_name[i][j] = './data/stu_data/' + str(dir_name[i]) + '/' + str(final_month_name[i][j])
        file_with_path = file_with_path + final_month_name[i]
    return file_with_path


"""
:param 用正则，选择排序
:return 按日期排序好的列表
"""


def sort_by_date(data, times):
    # 文件夹路径不同，选择不同匹配模式
    patt = '(\d+)-(\d+)' if times == 3 else '(\d+)-(\d+)-(\d+)'
    # 交换排序
    for i in range(len(data) - 1):
        for x in range(i + 1, len(data)):
            j = 1
            while j < times:
                lower = re.search(patt, data[i]).group(j)
                upper = re.search(patt, data[x]).group(j)
                # print lower,upper
                if int(lower) < int(upper):
                    j = times
                elif int(lower) == int(upper):
                    j += 1
                else:
                    data[i], data[x] = data[x], data[i]
                    j = times
                    
    return data


if __name__ == '__main__':
    file_name_with_path = filter_file_path()
    for i in file_name_with_path:
        print(i)
    print('count:', len(file_name_with_path))
